package com.tstudy.laoshibang.base;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import com.tstudy.laoshibang.R;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.TextView;

@EFragment(R.layout.webview_fragment)
public class WebViewFragment extends BaseFragment {
	private String mUrl = "";
	private String mTitle;
	final static String TAG = "webview_fragment";
	boolean mIsFirstLoad = true;//判断是否是第一次加载
	
	@ViewById(R.id.webview_fragment_root)
	View mRootLayout;
	
	@ViewById(R.id.webview_fragment_title)
	TextView mTitleTxt;
	
	@ViewById(R.id.webview_fragment_view)
	WebView mWebView;
	

	public static void add(int containerViewId,String url,String title){
		Bundle args = new Bundle();
		args.putString(CONSTANT.ARGS.URL, url);
		args.putString(CONSTANT.ARGS.TITLE, title);
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,WebViewFragment_.class.getName(), args);
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId,fragment,TAG);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup)mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		if (mIsFirstLoad) {
			initWebView();
			mTitleTxt.setText(mTitle);
			mWebView.loadUrl(mUrl);
		}
	}
	
	@Click(R.id.webview_fragment_back)
	public void back(){
		backAction(mFragmentId);
	}
	

	@SuppressLint("SetJavaScriptEnabled")
	@SuppressWarnings("deprecation")
	private void initWebView() {// 初始化
		mWebView.getSettings().setSupportZoom(true); // Zoom Control on web (You
		// don't need this
		mWebView.getSettings().setJavaScriptEnabled(true);// 可用JS
		mWebView.getSettings().setAllowContentAccess(true);
		mWebView.getSettings().setPluginState(PluginState.ON);
		mWebView.getSettings().setBuiltInZoomControls(true);
		mWebView.getSettings().setLoadsImagesAutomatically(true);
		mWebView.getSettings().setAllowFileAccess(true);
		mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
		// mWebView.getSettings()
		// .setUserAgentString(
		// "Mozilla/5.0 (Linux; U; Android 2.2.1; zh-cn; MB525 Build/3.4.2-117) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
		mWebView.setScrollBarStyle(0);// 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上
		mWebView.getSettings().setLoadWithOverviewMode(true);
		// sdk 11以上
		// mWebView.getSettings().setDisplayZoomControls(true);
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.getSettings().setDatabaseEnabled(true);
		mWebView.getSettings().setDomStorageEnabled(true);
		// mWebView.getSettings().setGeolocationEnabled(true);
		mWebView.getSettings().setAppCacheEnabled(true);
		mWebView.getSettings().setAppCacheMaxSize(10000);
		mWebView.setWebChromeClient(new WebChromeClient(){

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
				if (newProgress >= 100) {
				}
			}

			@Override
			public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
				return super.onJsAlert(view, url, message, result);
			}
			
		});
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mWebView.clearCache(true);
	}

	@Override
	public void initArgs(Bundle bundle) {
		if (bundle != null) {
			mUrl = bundle.getString(CONSTANT.ARGS.URL);
			mTitle = bundle.getString(CONSTANT.ARGS.TITLE);
			mFragmentId = bundle.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}


	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	
}
